package cn.tansanqinger.service.comment.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tansanqinger.dao.sql.comment.CommentTopicSqlDaoImpl;
import cn.tansanqinger.dao.sql.comment.CommentusertopicSqlDaoImpl;
import cn.tansanqinger.dao.sql.topic.TopicSqlDaoImpl;
import cn.tansanqinger.dao.sql.user.UserMessageDispose;
import cn.tansanqinger.pojo.Commenttopic;
import cn.tansanqinger.pojo.Commentusertopic;
import cn.tansanqinger.pojo.Topic;
import cn.tansanqinger.pojo.User;
import cn.tansanqinger.service.comment.ICommentTopicService;
import cn.tansanqinger.util.DateUtil;
import cn.tansanqinger.util.PageData;
import cn.tansanqinger.util.Tools;

@Service
public class CommentTopicServiceImpl implements ICommentTopicService{

	@Autowired
	private CommentusertopicSqlDaoImpl cuimpl;
	@Autowired
	private CommentTopicSqlDaoImpl cimpl;
	@Autowired
	private TopicSqlDaoImpl timpl;
	@Autowired
	private UserMessageDispose dispose;
	
	@Override
	public List<Commenttopic> selectCommentMessage(User user, int page, int tid) {
		List<Commenttopic> commenttopics = cimpl.selectCommentTopicOneTiter(tid, page);
		if(!commenttopics.isEmpty()) {
			for (int i = 0; i < commenttopics.size(); i++) {
				Commentusertopic commentusertopic = cuimpl.selectTidUidOne(commenttopics.get(i).getCid(), user.getId());
				if(commentusertopic!=null) {
					commenttopics.get(i).setCuid(commentusertopic.getCount());
				}
				commenttopics.get(i).setCnumber(cimpl.selectCountTwoTiter(commenttopics.get(i).getTid(), commenttopics.get(i).getCid()));
				user = dispose.selectOneId(commenttopics.get(i).getUid());
				if(user!=null) {
					commenttopics.get(i).setUname(user.getName());
					commenttopics.get(i).setUimg(user.getImg());
				}
			}
		}
		return commenttopics;
	}

	@Override
	public PageData add(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		Commenttopic commenttopic = new Commenttopic();
		commenttopic.setCpraise(0);
		commenttopic.setCtread(0);
		commenttopic.setTid(Integer.parseInt(request.getParameter("tid")));
		commenttopic.setCmessage(Tools.isEmpty(request.getParameter("message")));
		commenttopic.setCtiter(Integer.parseInt(Tools.isEmpty(request.getParameter("titer"))));
		commenttopic.setWuid(Integer.parseInt(Tools.isEmpty(request.getParameter("wuid"))));
		commenttopic.setCtime(System.currentTimeMillis()/1000);
		commenttopic.setCtid(Integer.parseInt(Tools.isEmpty(request.getParameter("ctid"))));
		commenttopic.setWcid(Integer.parseInt(Tools.isEmpty(request.getParameter("wcid"))));
		commenttopic.setCdate(DateUtil.getTime());
		commenttopic.setUid(user.getId());
		if(commenttopic.getWcid()!=0) {
			Commenttopic commenttopic2 = cimpl.selectCidOne(commenttopic.getWcid());
			if(commenttopic2!=null) {
				commenttopic2.setIsdispose(1);
				cimpl.modification(commenttopic2);
			}
		}
		PageData resultMap = new PageData();
		int cid = cimpl.add(commenttopic);
		if(cid>0) {
			Topic topic = timpl.selectOnt(Integer.parseInt(Tools.isEmpty(request.getParameter("tid"))));
			if(topic.getTcomment()==null) topic.setTcomment(1);
			else topic.setTcomment(topic.getTcomment()+1);
			timpl.modification(topic);
			resultMap.put("cid",cid);
			resultMap.put("uid",user.getId());
			resultMap.put("tid",commenttopic.getTid());
			resultMap.put("ctid",commenttopic.getCtid());
			resultMap.put("cdate",commenttopic.getCdate());
			resultMap.put("cuid",0);
			resultMap.put("cmessage",commenttopic.getCmessage());
			resultMap.put("cpraise",commenttopic.getCpraise());
			resultMap.put("cnumber",0);
			resultMap.put("uname",dispose.selectOneId(commenttopic.getUid()).getName());
			resultMap.put("code","0");
			resultMap.put("msg", "添加成功");
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "添加失败");
		}
		return resultMap;
	}

	@Override
	public PageData delete(String id) {
		PageData resultMap = new PageData();
		if(id.matches("\\d+")) {
			if(cimpl.delete(Integer.parseInt(id))==1) {
				resultMap.put("code","0");
				resultMap.put("msg", "删除成功");
			} else {
				resultMap.put("code","-1");
				resultMap.put("msg", "删除失败");
			}
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "数据错误");
		}
		return resultMap;
	}


	@Override
	public List<Commenttopic> selectCommentTwoTiterMessage(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		String page = Tools.isEmpty(request.getParameter("page"));
		String tid = Tools.isEmpty(request.getParameter("tid"));
		String ctid = Tools.isEmpty(request.getParameter("ctid"));
		if(!tid.matches("\\d+")) {
			return null;
		}
		int pageInt = 1;
		if(!"".equals(page)) {
			pageInt = Integer.parseInt(page);
		}
		List<Commenttopic> commenttopics = cimpl.selectCommentTopicTwoTiter(Integer.parseInt(tid), Integer.parseInt(ctid), pageInt);
		if(!commenttopics.isEmpty()) {
			for (int i = 0; i < commenttopics.size(); i++) {
				Commentusertopic commentusertopic = cuimpl.selectTidUidOne(commenttopics.get(i).getCid(), user.getId());
				if(commentusertopic!=null) {
					commenttopics.get(i).setCuid(commentusertopic.getCount());
				}
				user = dispose.selectOneId(commenttopics.get(i).getUid());
				commenttopics.get(i).setUname(user.getName());
				commenttopics.get(i).setUimg(user.getImg());
			}
		}
		return commenttopics;
	}

	@Override
	public int selectCountOneTiter(HttpServletRequest request) {
		return cimpl.selectCountOneTiter(Integer.parseInt(Tools.isEmpty(request.getParameter("cid"))));
	}

	@Override
	public int selectCountTwoTiter(HttpServletRequest request) {
		return cimpl.selectCountTwoTiter(Integer.parseInt(Tools.isEmpty(request.getParameter("cid"))),Integer.parseInt(Tools.isEmpty(request.getParameter("ctid"))));
	}

	@Override
	public PageData praiseSumbit(HttpServletRequest request) {
		String cid = Tools.isEmpty(request.getParameter("cid"));
		PageData resultMap = new PageData();
		User user = (User) request.getSession().getAttribute("user");
		if(cid.matches("\\d+")) {
			Commenttopic commenttopic = cimpl.selectCidOne(Integer.parseInt(cid));
			Commentusertopic commentusertopic = cuimpl.selectTidUidOne(Integer.parseInt(cid), user.getId());
			if(commentusertopic==null) {
				commentusertopic = new Commentusertopic();
				commentusertopic.setCount(1);
				if(commenttopic!=null) commentusertopic.setWuid(commenttopic.getUid());
				commentusertopic.setCid(Integer.parseInt(cid));
				commentusertopic.setUid(user.getId());
				commentusertopic.setCtime(System.currentTimeMillis()/1000);
				commentusertopic.setCdate(DateUtil.getTime());
				if(1==cuimpl.add(commentusertopic)) {
					if(commenttopic!=null) {
						if(commenttopic.getCpraise()==null) {
							commenttopic.setCpraise(1);
						} else {
							commenttopic.setCpraise(commenttopic.getCpraise()+1);
						}
					}
					cimpl.modification(commenttopic);
					resultMap.put("code","0");
					resultMap.put("msg", commenttopic.getCpraise());
					resultMap.put("ispraise", 1);//赞
				} else {
					resultMap.put("code","-1");
				}
			} else {
				if(commentusertopic.getCount()==1) {
					if(1==cuimpl.delete(commentusertopic.getCuid())) {
						if(commenttopic!=null) {
							if(commenttopic.getCpraise()==null) {
								commenttopic.setCpraise(0);
							} else {
								commenttopic.setCpraise(commenttopic.getCpraise()-1);
							}
						}
						cimpl.modification(commenttopic);
						resultMap.put("code","0");
						resultMap.put("msg", commenttopic.getCpraise());
						resultMap.put("ispraise", 2);//取消
					}
				} else {
					commentusertopic.setCount(1);
					if(1==cuimpl.modification(commentusertopic)) {
						if(commenttopic!=null) {
							if(commenttopic.getCpraise()==null) {
								commenttopic.setCpraise(1);
							} else {
								commenttopic.setCpraise(commenttopic.getCpraise()+1);
								commenttopic.setCtread(commenttopic.getCtread()-1);
							}
						}
						cimpl.modification(commenttopic);
						resultMap.put("code","0");
						resultMap.put("msg", commenttopic.getCpraise());
						resultMap.put("ispraise", 1);
					}
				}
			}
		}
				
		return resultMap;
	}

	@Override
	public PageData treadSumbit(HttpServletRequest request) {
		String cid = Tools.isEmpty(request.getParameter("cid"));
		PageData resultMap = new PageData();
		User user = (User) request.getSession().getAttribute("user");
		if(cid.matches("\\d+")) {
			Commentusertopic commentusertopic = cuimpl.selectTidUidOne(Integer.parseInt(cid), user.getId());
			if(commentusertopic==null) {
				commentusertopic = new Commentusertopic();
				commentusertopic.setCount(2);
				commentusertopic.setCid(Integer.parseInt(cid));
				commentusertopic.setUid(user.getId());
				if(1==cuimpl.add(commentusertopic)) {
					Commenttopic commenttopic = cimpl.selectCidOne(Integer.parseInt(cid));
					commenttopic.setCtread(commenttopic.getCtread()+1);
					cimpl.modification(commenttopic);
					resultMap.put("code","0");
					resultMap.put("msg", commenttopic.getCpraise());
					resultMap.put("istread", 1);//踩
				} else {
					resultMap.put("code","-1");
				}
			} else {
				if(commentusertopic.getCount()==2) {
					if(1==cuimpl.delete(commentusertopic.getCuid())) {
						Commenttopic commenttopic = cimpl.selectCidOne(Integer.parseInt(cid));
						cimpl.modification(commenttopic);
						resultMap.put("code","0");
						resultMap.put("msg", commenttopic.getCpraise());
						resultMap.put("istread", 2);//取消
					}
				} else {
					commentusertopic.setCount(2);
					if(1==cuimpl.modification(commentusertopic)) {
						Commenttopic commenttopic = cimpl.selectCidOne(Integer.parseInt(cid));
						commenttopic.setCtread(commenttopic.getCtread()+1);
						commenttopic.setCpraise(commenttopic.getCpraise()-1);
						cimpl.modification(commenttopic);
						cimpl.modification(commenttopic);
						resultMap.put("code","0");
						resultMap.put("msg", commenttopic.getCpraise());
						resultMap.put("istread", 1);
					}
				}
			}
		}
		return resultMap;
	}

	@Override
	public List<Commenttopic> selectUserAll(HttpServletRequest request) {
		String page = Tools.isEmpty(request.getParameter("page"));
		User user = (User) request.getSession().getAttribute("user");
		if(page.matches("\\d+")) {
			return cimpl.selectUserAll(user.getId(), Integer.parseInt(page));
		} else {
			return cimpl.selectUserAll(user.getId(), 1);
		}
	}

	@Override
	public int selectUserAllNumber(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		return cimpl.selectUserAllNumber(user.getId());
	}

	@Override
	public List<Commenttopic> replyToComment(HttpServletRequest request) {
		String page = Tools.isEmpty(request.getParameter("page"));
		User user = (User) request.getSession().getAttribute("user");
		List<Commenttopic> commenttopics = null;
		if(page.matches("\\d+")) {
			commenttopics = cimpl.replyToComment(user.getId(), Integer.parseInt(page));
		} else {
			commenttopics = cimpl.replyToComment(user.getId(), 1);
		}
		for (int i = 0; i < commenttopics.size(); i++) {
			User user2 = dispose.selectOneId(commenttopics.get(i).getUid());
			if(user2!=null) {
				commenttopics.get(i).setUname(user2.getName());
				commenttopics.get(i).setUimg(user2.getImg());
			}
			Commenttopic commenttopic = cimpl.selectCidOne(commenttopics.get(i).getWcid());
			if(commenttopic!=null) {
				commenttopics.get(i).setTmessage(commenttopic.getCmessage());
			}
		}
		return commenttopics;
	}

	@Override
	public int replyToCommentCountAll(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		return cimpl.replyToCommentCountAll(user.getId());
	}

	@Override
	public int replyToCommentCountIsSelect(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		return cimpl.replyToCommentCountIsSelect(user.getId());
	}

	@Override
	public List<Commentusertopic> praiseComment(HttpServletRequest request) {
		String page = Tools.isEmpty(request.getParameter("page"));
		User user = (User) request.getSession().getAttribute("user");
		List<Commentusertopic> commentusertopics = null;
		if(page.matches("\\d+")) {
			commentusertopics = cuimpl.replyToComment(user.getId(), Integer.parseInt(page));
		} else {
			commentusertopics = cuimpl.replyToComment(user.getId(), 1);
		}
		for (int i = 0; i < commentusertopics.size(); i++) {
			User user2 = dispose.selectOneId(commentusertopics.get(i).getUid());
			if(user2!=null) {
				commentusertopics.get(i).setUname(user2.getName());
				commentusertopics.get(i).setUimg(user2.getImg());
			}
			Commenttopic commenttopic = cimpl.selectCidOne(commentusertopics.get(i).getCid());
			if(commenttopic!=null) {
				commentusertopics.get(i).setUmessage(commenttopic.getCmessage());
			}
		}
		return commentusertopics;
	}

	@Override
	public int praiseCommentCountIsSelect(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		return cuimpl.praiseCountIsSelect(user.getId());
	}

	@Override
	public int praiseCountAll(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		return cuimpl.praiseCountAll(user.getId());
	}

	@Override
	public PageData replyComment(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		String cid = Tools.isEmpty(request.getParameter("id"));
		PageData pd = new PageData();
		if(cid.matches("\\d+")) {
			Commenttopic commenttopic = cimpl.selectCidOne(Integer.parseInt(cid));
			Commenttopic commenttopic2 = new Commenttopic();
			commenttopic2.setCpraise(0);
			commenttopic2.setCtread(0);
			commenttopic2.setTid(commenttopic.getTid());
			commenttopic2.setCmessage(Tools.isEmpty(request.getParameter("message")));
			commenttopic2.setCtiter(2);
			commenttopic2.setWuid(commenttopic.getUid());
			commenttopic2.setCtime(System.currentTimeMillis()/1000);
			if(commenttopic.getCtid()==0) {
				commenttopic2.setCtid(commenttopic.getCid());
			} else {
				commenttopic2.setCtid(commenttopic.getCtid());
			}
			commenttopic2.setWcid(commenttopic.getCid());
			commenttopic2.setCdate(DateUtil.getTime());
			commenttopic2.setUid(user.getId());
			commenttopic.setIsdispose(1);
			cimpl.modification(commenttopic);
			if(cimpl.add(commenttopic2)>0) {
				pd.put("code","0");
				pd.put("msg", "回复成功");
			} else {
				pd.put("code","-1");
				pd.put("msg", "回复失败");
			}
		} else {
			pd.put("code", "-1");
			pd.put("msg", "数据错误");
		}
		return pd;
	}

}
